//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#pragma link "ZConnect"
#pragma link "ZMySqlCon"
#pragma link "ZMySqlQuery"
#pragma link "ZMySqlTr"
#pragma link "ZQuery"
#pragma link "ZTransact"

#include "VerifBase.h"

//---------------------------------------------------------------------------
//              CONSTRUCTEUR
//---------------------------------------------------------------------------
__fastcall CVerifBase::CVerifBase( void )
{

    TZZMySqlDatabase * Base = NULL;
    Base = new TZZMySqlDatabase( (TComponent*)0);
    Base->Database = BASE_WEBHELP;
    Base->Login = LOGIN;
    Base->Host = ADRESSE_IP;
    Base->Port = PORT;
    try {
        Base->Connected = true;
    }catch (Exception &e) {
        Base->Connected = false;
        ShowMessage("Erreur de connection à la base");
    }
    if(Base) {delete Base; Base = NULL; }
}

//---------------------------------------------------------------------------
//              VERIFIER LES TABLES
//---------------------------------------------------------------------------
bool __fastcall CVerifBase::VerifierTables( void )
{
    bool bRetour = false;
    if(     IsTablesExist("logiciels")
        &&  IsTablesExist("refer")
        &&  IsTablesExist("versions"))
    {
        bRetour = true;
    }
    else {
        //Creation des tables
        ExecuterScript(SCRIPT_WEBHELP);
    }
    return bRetour;
}
//---------------------------------------------------------------------------
//              EXISTANCES DES TABLES
//---------------------------------------------------------------------------
bool __fastcall CVerifBase::IsTablesExist(const AnsiString &NomTable )
{
    bool bRetour = false;

    TZZMySqlDatabase * BaseSystem = NULL;
    BaseSystem = new TZZMySqlDatabase( (TComponent*)0);
    BaseSystem->Database = INFORMATION_SCHEMA;
    BaseSystem->Login = LOGIN;
    BaseSystem->Host  = ADRESSE_IP;
    BaseSystem->Port  = PORT;
    BaseSystem->Connected = true;

    TZZMySqlTransact * Transaction = NULL;
    Transaction = new TZZMySqlTransact((TComponent*)0);
    Transaction->Database   = BaseSystem;
    Transaction->AutoCommit = true;
    Transaction->Connected  = true;

    TZZMySqlQuery * MaRequete = NULL;
    MaRequete = new TZZMySqlQuery((TComponent*)0);
    MaRequete->Database = BaseSystem;
    MaRequete->Transaction = Transaction;

    MaRequete->Sql->Text =  (AnsiString)    "SELECT count( table_name ) "+
                                            "FROM TABLES "+
                                            "WHERE table_name = "+QuotedStr(NomTable);
    MaRequete->Open();
    MaRequete->First();
    if(MaRequete->Fields->Fields[0]->AsInteger != 0)
    {
        bRetour = true;
    }

    MaRequete->Sql->Clear();
    MaRequete->Close();
    MaRequete->Transaction->Connected = false;

    BaseSystem->Connected = false;

    if(MaRequete) {delete MaRequete; MaRequete = NULL;}
    if(Transaction) {delete Transaction; Transaction = NULL;}
    if(BaseSystem) {delete BaseSystem; BaseSystem = NULL; }
    return bRetour;
}

//---------------------------------------------------------------------------
//              EXECUTION DU METADATA
//---------------------------------------------------------------------------
void __fastcall CVerifBase::ExecuterScript(const AnsiString &NomMetadata )
{
    TZZMySqlDatabase * Base = NULL;
    Base = new TZZMySqlDatabase( (TComponent*)0);
    Base->Database = BASE_WEBHELP;
    Base->Login = LOGIN;
    Base->Host = ADRESSE_IP;
    Base->Port = PORT;
    Base->Connected = true;

    //creation de la base grace au metadata.sql
    TZZMySqlTransact * Transaction = NULL;
    Transaction = new TZZMySqlTransact((TComponent*)0);
    Transaction->Database = Base;
    Transaction->AutoCommit = true;
    Transaction->Connected = true;

    TZZBatchSql * MySql_Script = NULL;
    MySql_Script = new TZZBatchSql( (TComponent*)0 );
    MySql_Script->Transaction = Transaction;
    MySql_Script->Sql->LoadFromFile(NomMetadata);
    if(FileExists(NomMetadata))
    {
        MySql_Script->ExecSql();
        MySql_Script->Sql->Clear();
    }


    MySql_Script->Transaction->Connected = false;
    Base->Connected = false;
    if(MySql_Script) {delete MySql_Script; MySql_Script = NULL;}
    if(Transaction) {delete Transaction; Transaction = NULL;}
    if(Base) {delete Base; Base = NULL; }
}
